rm(list = setdiff(ls(), lsf.str()))

#Load data 2017
election17<-haven::read_dta("Study 1/Original Data/ni17a_EN_1.0.dta") 
personality17<-haven::read_dta("Study 1/Original Data/cp17i_EN_1.0p.dta")
politics17<-haven::read_dta("Study 1/Original Data/cv17i_EN_1.0p.dta")
background17<-haven::read_dta("Study 1/Original Data/avars_201703_EN_1.0p.dta")

#merge data
data_1 <- merge(election17, personality17,by="nomem_encr", all=T)
data_2 <- merge(data_1, politics17,by="nomem_encr", all=T)
data <- merge(data_2, background17,by="nomem_encr")

data <- data[,which(colnames(data)%in%c("nomem_encr", "ni17a003", "ni17a005", "oplcat", "nettocat", "geslacht", "leeftijd", "cv17i101", "cv17i104", "cv17i103", "cv17i047", "cv17i048", "cv17i049", "cp17i020", "cp17i021", "cp17i022", "cp17i023", "cp17i024", "cp17i025", "cp17i026", "cp17i027", "cp17i028", "cp17i029", "cp17i030", "cp17i031", "cp17i032", "cp17i033", "cp17i034", "cp17i035", "cp17i036", "cp17i037", "cp17i038", "cp17i039", "cp17i040", "cp17i041", "cp17i042", "cp17i043", "cp17i044", "cp17i045", "cp17i046", "cp17i047", "cp17i048", "cp17i049", "cp17i050", "cp17i051", "cp17i052", "cp17i053", "cp17i054", "cp17i055", "cp17i056", "cp17i057", "cp17i058", "cp17i059", "cp17i060", "cp17i061", "cp17i062", "cp17i063", "cp17i064", "cp17i065", "cp17i066", "cp17i067", "cp17i068", "cp17i069"))]

#did not vote
data$didnotvote<-ifelse(data$ni17a003==2,1,0)

#Code variables ---------
#PVV, GeenPeil, FvD,  Voor Nederland, Nieuwe Wegen
data$populist<-ifelse(data$ni17a005==3 | data$ni17a005==13 | data$ni17a005==14| data$ni17a005==15| data$ni17a005==17, 1,0)
data$populist[is.na(data$populist)]=0 #set missing responses to 0

data$populist_pvv<-ifelse(data$ni17a005==3, 1,0)
data$populist_pvv[is.na(data$populist_pvv)]=0 #set missing responses to 0

#POpuslit, other, did not vote
data$populist_other_not<-data$populist
data$populist_other_not[data$didnotvote==1]=2

#Multinomial
data$populist_govopp<- car::recode(data$ni17a005, "1=2; 2=2; 3=1; 4=3; 5=3; 6=3; 7=3; 8=3; 9=3; 10=3; 11=3; 12=3; 13=1; 14=1; 15=1; 16=3; 18=3; 20=3; 21=3; 22=3; 23=3; 17=1")
data$populist_govopp<-as.factor(data$populist_govopp)
levels(data$populist_govopp) <- c("populist","Government","Opposition")
data$populist_govopp <- relevel(data$populist_govopp, ref = "populist")

#Education
data$education<-data$oplcat

#Income
data$income<-zero1(car::recode(data$nettocat, "13=NA; 14=NA"))
data$income[is.na(data$income)==TRUE]=2
data$income_missing<-ifelse(data$income==2,1,0) 

#sex
data$female<-ifelse(data$geslacht==2,1,0)

#age
data$age<-data$leeftijd

#left-right
data$lr_placement<-car::recode(data$cv17i101, "999=NA")
data$lr_placement<-zero1(data$lr_placement)
data$lr_placement[is.na(data$lr_placement)==TRUE]=2
data$lr_placement_missing<-ifelse(data$lr_placement==2, 1,0)

#Social conservatism
data$anti_immi<-car::recode(data$cv17i104, "99=NA")

#economic conservatism
data$econ_cons<-6-(car::recode(data$cv17i103, "99=NA"))

#cynicism
data$cyn1<-ifelse(data$cv17i047==2,1,0)
data$cyn2<-ifelse(data$cv17i048==2,1,0)
data$cyn3<-ifelse(data$cv17i049==2,1,0)
data$cynicism<-rowMeans(data.frame(data$cyn1,data$cyn2,data$cyn3),na.rm=T)

##############################
#
#
#IPIP FFM (50 items)
#
#
##############################
#Openness in 2017
data$y17_open1 <-data$cp17i024
data$y17_open2 <-data$cp17i034 
data$y17_open3 <-data$cp17i044
data$y17_open4 <-data$cp17i054
data$y17_open5 <-data$cp17i059
data$y17_open6 <-data$cp17i064
data$y17_open7 <-data$cp17i069
#reversed coded items
data$y17_rec_open8 <-car::recode(as.numeric(data$cp17i029),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_open9 <-car::recode(as.numeric(data$cp17i039),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_open10 <-car::recode(as.numeric(data$cp17i049),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$open<-rowMeans(data.frame(data$y17_open1,data$y17_open2,data$y17_open3,data$y17_open4,data$y17_open5,data$y17_open6, data$y17_open7, data$y17_rec_open8, data$y17_rec_open9, data$y17_rec_open10),na.rm=T)

#Conscientiousness
data$y17_con1 <- data$cp17i022
data$y17_con2 <- data$cp17i032
data$y17_con3 <- data$cp17i042
data$y17_con4 <- data$cp17i052
data$y17_con5 <- data$cp17i062
data$y17_con6 <- data$cp17i067
#reverse coded items
data$y17_rec_con7 <-car::recode(as.numeric(data$cp17i027),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_con8 <-car::recode(as.numeric(data$cp17i037),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_con9 <-car::recode(as.numeric(data$cp17i047),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_con10 <-car::recode(as.numeric(data$cp17i057),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$con<-rowMeans(data.frame(data$y17_con1,data$y17_con2,data$y17_con3,data$y17_con4,data$y17_con5,data$y17_con6, data$y17_rec_con7, data$y17_rec_con8, data$y17_rec_con9, data$y17_rec_con10),na.rm=T)

#Extraversion
#*2017
data$y17_ext1 <- data$cp17i020
data$y17_ext2 <- data$cp17i030
data$y17_ext3 <- data$cp17i040
data$y17_ext4 <- data$cp17i050
data$y17_ext5 <- data$cp17i060
#reversed coded items
data$y17_rec_ext6 <-car::recode(as.numeric(data$cp17i025),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_ext7 <-car::recode(as.numeric(data$cp17i035),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_ext8 <-car::recode(as.numeric(data$cp17i045),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_ext9 <-car::recode(as.numeric(data$cp17i055),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_ext10 <-car::recode(as.numeric(data$cp17i065),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$ext<-rowMeans(data.frame(data$y17_ext1,data$y17_ext2,data$y17_ext3,data$y17_ext4,data$y17_ext5,data$y17_rec_ext6, data$y17_rec_ext7, data$y17_rec_ext8, data$y17_rec_ext9, data$y17_rec_ext10),na.rm=T)

#Agreeableness
data$y17_agre1 <- data$cp17i026
data$y17_agre2 <- data$cp17i036
data$y17_agre3 <- data$cp17i046
data$y17_agre4 <- data$cp17i056
data$y17_agre5 <- data$cp17i061
data$y17_agre6 <- data$cp17i066
#reversed coded items
data$y17_rec_agre7 <-car::recode(as.numeric(data$cp17i021),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_agre8 <-car::recode(as.numeric(data$cp17i031),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_agre9 <-car::recode(as.numeric(data$cp17i041),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_agre10 <-car::recode(as.numeric(data$cp17i051),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$agre<-rowMeans(data.frame(data$y17_agre1,data$y17_agre2,data$y17_agre3,data$y17_agre4,data$y17_agre5,data$y17_agre6, data$y17_rec_agre7, data$y17_rec_agre8, data$y17_rec_agre9, data$y17_rec_agre10),na.rm=T)


#Neuroticism
#2017
data$y17_neu1 <- data$cp17i023
data$y17_neu2 <- data$cp17i033
data$y17_neu3 <- data$cp17i043
data$y17_neu4 <- data$cp17i048
data$y17_neu5 <- data$cp17i053
data$y17_neu6 <- data$cp17i058
data$y17_neu7 <- data$cp17i063
data$y17_neu8 <- data$cp17i068
#reversed coded items
data$y17_rec_neu9 <-car::recode(as.numeric(data$cp17i028),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y17_rec_neu10 <-car::recode(as.numeric(data$cp17i038),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$neu<-rowMeans(data.frame(data$y17_neu1,data$y17_neu2,data$y17_neu3,data$y17_neu4,data$y17_neu5,data$y17_neu6, data$y17_neu7, data$y17_neu8, data$y17_rec_neu9, data$y17_rec_neu10),na.rm=T)

#Education
data$edu1<-ifelse(data$education==1,1,0)
data$edu2<-ifelse(data$education==2,1,0)
data$edu3<-ifelse(data$education==3,1,0)
data$edu4<-ifelse(data$education==4,1,0)
data$edu5<-ifelse(data$education==5,1,0)
data$edu6<-ifelse(data$education==6,1,0)

#save data
save(data, file="Study 1/Altered Data/Study1_NL_17.RData")

